//
//  MYRequesTool.m
//  MiaoYin
//
//  Created by DarkWorld on 2020/1/4.
//  Copyright © 2020 thy. All rights reserved.
//

#import "MYRequesTool.h"

@implementation MYRequesTool


+ (BOOL)requestReslutDelWith:(NSDictionary *)result
{
    int code = [result[@"code"] intValue];
    if (code == 401)
    {
      
            //失败 授权失败请重新登录
           NSString *alterContent = @"提示";
           UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alterContent message:@"账号token验证失效，请重新登录！" preferredStyle:(UIAlertControllerStyleAlert)];
           UIAlertAction *action = [UIAlertAction actionWithTitle:@"重新登录" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
               [MiaoYinTools logout];
           }];
           UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
               
           }];
           [alertController addAction:cancel];
           [alertController addAction:action];
            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
            return false;
    }
    else if(code == 200)
    {
        int status = [result[@"status"] intValue];
        if(1000<status && status<4000)
        {
            if (status == 1030 || status == 1031 || status == 1032)
            {
                //        1030 ----- 邀请码有误
                //        1031 ----- 已存在推荐关系，请勿重复操作
                //        1032 ----- 不能多次建立推荐关系
                return true;
            }
            else
            {
                NSString *msg = result[@"message"];
                NSLog(@"----%@",result);
                msg = msg&&msg.length>0?msg:DefultLodErrorString;
                [MYTipUtils showErrorAlertString:msg];
                return false;
            }
        }
        else
        {
            switch (status) {
                        case 0:
                        {
                            //成功
                            return true;
                        }
                            break;

                        default:
                        {
                            NSLog(@"----%@",result);
                            [MYTipUtils showErrorAlertString:DefultLodErrorString];
                           return false;
                        }
                            break;
                    }
        }
        
    }
    else
    {
        [MYTipUtils showErrorAlertString:DefultLodErrorString];
        return false;
    }
    
}
/// 封装综合请求 目前仅阿里短视频使用  jsonbody传值
/// @param urlString 后缀地址
/// @param requestType 请求方式
/// @param MYServiceModuleType 请求模块
/// @param params 请求参数
/// @param successHandle 成功回调
/// @param failureHandle 失败回调
+ (void)requestBodyUrl:(NSString *)urlString
       requestType:(MYrequestType)requestType
MYServiceModuleType:(MYServiceModuleType)MYServiceModuleType
            params:(NSString *)params
     successHandle:(void(^)(id response))successHandle
     failureHandle:(void(^)(NSError *error))failureHandle
{
    
    NSString *requestTypes = @"POST";
    switch (requestType) {
      
        case MYrequestType_Get:
            requestTypes = @"GET";
            break;
        case MYrequestType_Post:
            requestTypes = @"POST";
            break;
        case MYrequestType_Put:
            requestTypes = @"PUT";
            break;
    }
    [DWHTTPManager  postJsonAFNWithUrl:urlString parameters:params requestType:requestTypes MYServiceModuleType:MYServiceModuleType successHandle:^(NSURLSessionTask * _Nonnull task, id  _Nonnull response) {
         DLog(@"%@",response);
        if(MYServiceModuleType == MYServiceModule_normal)
        {
            if (successHandle) {
                successHandle(response);
            }
        }
        else
        {
            if ([self requestReslutDelWith:response]) {
                  if (successHandle) {
                      successHandle(response);
                  }
              }
              else
              {
                  if (failureHandle) {
                      failureHandle(nil);
                  }
              }
        }
       
         
     } failureHandle:^(NSURLSessionTask * _Nonnull task, NSError * _Nonnull error) {
         DLog(@"%@",error);

         if(failureHandle) {
             failureHandle(error);
         }
     }];
}
/// 封装综合请求
/// @param urlString 后缀地址
/// @param requestType 请求方式
/// @param MYServiceModuleType 请求模块
/// @param params 请求参数
/// @param successHandle 成功回调
/// @param failureHandle 失败回调
+ (void)requestUrl:(NSString *)urlString
       requestType:(MYrequestType)requestType
MYServiceModuleType:(MYServiceModuleType)MYServiceModuleType
            params:(NSDictionary *)params
     successHandle:(void(^)(id response))successHandle
     failureHandle:(void(^)(NSError *error))failureHandle
{
    switch (requestType) {
        
        case MYrequestType_Get:
        {
            [DWHTTPManager  getAFNWithUrl:urlString parameters:params MYServiceModuleType:MYServiceModuleType successHandle:^(NSURLSessionTask * _Nonnull task, id  _Nonnull response) {
                 DLog(@"%@",response);
                if(MYServiceModuleType == MYServiceModule_normal)
                {
                    if (successHandle) {
                        successHandle(response);
                    }
                }
                else
                {
                    if ([self requestReslutDelWith:response]) {
                          if (successHandle) {
                              successHandle(response);
                          }
                      }
                      else
                      {
                          if (failureHandle) {
                              failureHandle(nil);
                          }
                      }
                }
               
                 
             } failureHandle:^(NSURLSessionTask * _Nonnull task, NSError * _Nonnull error) {
                 DLog(@"%@",error);

                 if(failureHandle) {
                     failureHandle(error);
                 }
             }];
        }
            break;
        case MYrequestType_Post:
        {
            [DWHTTPManager  postAFNWithUrl:urlString parameters:params MYServiceModuleType:MYServiceModuleType successHandle:^(NSURLSessionTask * _Nonnull task, id  _Nonnull response) {
                 DLog(@"%@",response);

                int status = [response[@"status"] intValue];
               if ([self requestReslutDelWith:response]) {
                     if (successHandle) {
                         successHandle(response);
                     }
                 }
                 else
                 {
                     if (failureHandle) {
                         failureHandle(nil);
                     }
                 }
                 
             } failureHandle:^(NSURLSessionTask * _Nonnull task, NSError * _Nonnull error) {
                 DLog(@"%@",error);

                 if(failureHandle) {
                     failureHandle(error);
                 }
             }];
        }
            break;
        case MYrequestType_Put:
        {
            [DWHTTPManager  putAFNWithUrl:urlString parameters:params MYServiceModuleType:MYServiceModuleType successHandle:^(NSURLSessionTask * _Nonnull task, id  _Nonnull response) {
                 DLog(@"%@",response);

                int status = [response[@"status"] intValue];
               if ([self requestReslutDelWith:response]) {
                     if (successHandle) {
                         successHandle(response);
                     }
                 }
                 else
                 {
                     if (failureHandle) {
                         failureHandle(nil);
                     }
                 }
                 
             } failureHandle:^(NSURLSessionTask * _Nonnull task, NSError * _Nonnull error) {
                 DLog(@"%@",error);

                 if(failureHandle) {
                     failureHandle(error);
                 }
             }];
        }
            break;
    }
}


@end
